package admin.server.controller;


import admin.server.service.UmsAdminService;
import admin.server.service.UmsResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import admin.common.vo.PageVo;
import admin.common.vo.ResponseVO;
import admin.dao.model.UmsResource;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 后台资源管理Controller
 * Created by zgb on 2020/2/4.
 */
@Controller
@Tag(name = "后台资源管理")
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService resourceService;
    @Autowired
    private UmsAdminService adminService;

    @Operation(summary = "添加后台资源")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO create(@RequestBody UmsResource umsResource) throws Exception {
        int count = resourceService.create(umsResource);
        if (count > 0) {
            return ResponseVO.success(count);
        } else {
            return ResponseVO.failed();
        }
    }

    @Operation(summary = "修改后台资源")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO update(@PathVariable Long id,
                               @RequestBody UmsResource umsResource) throws Exception {
        int count = resourceService.update(id, umsResource);
        if (count > 0) {
            return ResponseVO.success(count);
        } else {
            return ResponseVO.failed();
        }
    }

    @Operation(summary = "根据ID获取资源详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<UmsResource> getItem(@PathVariable Long id) {
        UmsResource umsResource = resourceService.getItem(id);
        return ResponseVO.success(umsResource);
    }

    @Operation(summary = "根据ID删除后台资源")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO delete(@PathVariable Long id) {
        resourceService.delete(id);
        return ResponseVO.success();
    }

    @Operation(summary = "分页模糊查询后台资源")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<PageVo<UmsResource>> list(@RequestParam(required = false) Long menuId,
                                                  @RequestParam(required = false) Integer resourceType,
                                                  @RequestParam(required = false) String nameKeyword,
                                                  @RequestParam(required = false) String urlKeyword,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageVo<UmsResource> page = resourceService.list(menuId,resourceType, nameKeyword, urlKeyword, pageSize, pageNum);
        return ResponseVO.success(page);
    }



    @Operation(summary = "查询所有后台资源")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<List<UmsResource>> listAll() {
        List<UmsResource> resourceList = resourceService.listAll();
        return ResponseVO.success(resourceList);
    }


    @Operation(summary = "查询当前用户后台资源")
    @RequestMapping(value = "/listCurrent", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<List<UmsResource>> listCurrent(HttpServletRequest request,@RequestParam(required = false) Long menuId) throws Exception {
        List<UmsResource> resourceList = adminService.getResourceList(adminService.getCurrentAdmin(request).getId(),menuId);
        return ResponseVO.success(resourceList);
    }

    @Operation(summary = "初始化资源角色关联数据")
    @RequestMapping(value = "/initResourceRolesMap", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO initResourceRolesMap() {
        Map<String, Set<String>> resourceRolesMap = resourceService.initResourceRolesMap();
        return ResponseVO.success(resourceRolesMap);
    }
}
